package com.hnevc.mytrafficdemo.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.hnevc.mytrafficdemo.R;
import com.hnevc.mytrafficdemo.adapter.MyBusCapacityAdapter;
import com.hnevc.mytrafficdemo.entity.BusStatus;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 公交车载情况统计
 */
public class BusCapacityDialog extends AlertDialog {
    Context mContext;
    ListView mListView;
    List<BusStatus> mBusList;
    private MyBusCapacityAdapter mAdapter;
    Button mBtnBack;
    int sum=0;

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            mTvTotoal.setText(sum+"人");
            if (msg.what == 101) {
                if (mListView != null) {
                    if (mAdapter != null) {
                        mAdapter.notifyDataSetChanged();
                    } else {
                        mAdapter = new MyBusCapacityAdapter(mBusList, mContext);
                        mListView.setAdapter(mAdapter);
                    }
                }
            }
        }
    };
    private Timer mTimer;
    private TextView mTvTotoal;


    protected BusCapacityDialog(@NonNull Context context) {
        super(context);
        mContext = context;


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bus_capacity);
        initView();
        initData();
    }

    private void initData() {
        if (mBusList == null) {
            mBusList = new ArrayList<>();
        }

        for (int i = 0; i < 15; i++) {
            BusStatus bus = new BusStatus();
            bus.setBusId(i + 1);
            bus.setCapacity(101);
            mBusList.add(i, bus);
        }
        MyBusCapacityAdapter mAdapter = new MyBusCapacityAdapter(mBusList, mContext);
        mListView.setAdapter(mAdapter);

        //定时器，每个秒获取一次数据
        mTimer = new Timer();
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                sum=0;
                for (int i = 0; i < 15; i++) {
                    int capacity = new Random().nextInt(101);
                    mBusList.get(i).setCapacity(capacity);
                    sum =sum+capacity;
                }
                mHandler.sendEmptyMessage(101);
            }
        }, 0, 3000);

    }

    private void initView() {
        mListView = (ListView) findViewById(R.id.id_lv_bus_capacity);
        //设置头部布局
        mListView.addHeaderView(LayoutInflater.from(mContext)
                .inflate(R.layout.bus_capacity_head_layout, null));
        //设置尾部布局
        mListView.addFooterView(LayoutInflater.from(mContext)
                .inflate(R.layout.bus_capacity_footer_layout, null));
        mBtnBack = (Button) findViewById(R.id.id_btn_back);
        mBtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTimer.cancel();
                cancel();
            }
        });
        mTvTotoal = (TextView) findViewById(R.id.id_tv_bus_total_capacity);


    }

}
